#CACHE{0}
<?php
    include_spip('functions/pdk_common');
    $cart = pdk_load_class('Alohacart','classes');
    $product_mdl = pdk_load_class('ProductModel','classes');
    
    $cart_data = array();
    $cart_products = $cart->GetProducts();
    
    if ($_REQUEST) {
        // Add product
        if (_request('act') == 'add_product' && $id_article = _request('id_article')) {
            if (lire_config('alohacart/config_consider_quantity')) {
                $product = $product_mdl->select_by_id($id_article);    
                if ($cart_products[$id_article] + 1 > $product['quantity']) {
                    $cart_data['not_enough_products'] = true;
                    exit(json_encode($cart_data));
                }
            }
            $cart->AddProduct($id_article);
        }
        
        // Clear cart
        if (_request('act') == 'clear_cart') {            
            $cart->ClearCart();
            exit('DeleteOK');
        }
        
    }
    
    // By default just return cart data
    $cart_quantity = $cart->CountProducts();
    $cart_price = $cart->CountPrice();
    $cart_data['cart_quantity'] = $cart_quantity; 
    $cart_data['cart_price'] = $cart_price;
    exit(json_encode($cart_data));
?>